<?php
/**
 * Handler File Class
 *
 * @author liliang <liliang@wolive.cc>
 * @email liliang@wolive.cc
 * @date 2017/06/01
 */

namespace app\admin\controller;

use app\admin\model\Admins;
use think\Controller;
use think\captcha\Captcha;
use think\config;
use app\Common;
use app\extra\push\Pusher;


/**
 * 登录控制器.
 */
class Login extends Controller
{

    /**
     * 登陆首页.
     *
     * @return string
     */
    public function index()
    {

        // 未登陆，呈现登陆页面.
        $params = [];
        $goto = $this->request->get('goto', '');
        if ($goto) {
            $params['goto'] = urlencode($goto);
        }

        $this->assign('submit', url('check', $params));
        return $this->fetch();
    }

    /**
     * 登陆等待页.
     *
     * @return string
     */
    public function loading()
    {
        return $this->fetch();
    }

    /**
     * 验证码.
     *
     * @return \think\Response
     */
    public function captcha()
    {

        $captcha = new Captcha(Config::get('captcha'));
        return $captcha->entry('admin_login');
    }

    /**
     * 检查.
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function check()
    {

        $post = $this->request->post();

        if (!isset($post['username']) || !isset($post['password'])) {
            $this->error('参数不完整!', url("/admin/login/index"));
        }

        $post['user_name'] = htmlspecialchars($post['username']);

        $post["password"] = htmlspecialchars($post['password']);
        unset($post['username']);

        if (!isset($post['is_merchant'])) {
            $result = $this->validate($post, 'Login');
            if ($result !== true) {
                $this->error($result);
            }
        }

        // 获取信息 根据$post['username'] 的数据 来做条件 获取整条信息
        $admin = Admins::table("wolive_service")->where('user_name', $post['user_name'])->find();
        if (!$admin) {
            $this->error("用户不存在");
        }

        // 获取登陆数据

        $login = $admin->getData();
        // 密码检查
        $salt = $login['salt'];
        $pass = md5($post["password"] . $salt);

        $password = Admins::table("wolive_service")->where('password', $pass)->find();

        if (!$password) {
            $this->error('密码错误');
        }

        // 删掉登录用户的敏感信息
        unset($login['password']);

        $res = Admins::table('wolive_service')->where('service_id', $login['service_id'])->update(['state' => 'online']);

        $data = Admins::table('wolive_service')->where('service_id', $login['service_id'])->find();

        $_SESSION['Msg'] = $data->getData();

        $common = new Common();

        $ismoblie = $common->isMobile();

        if ($ismoblie) {

            $this->success('登录成功', url("mobile/admin/index"));
        } else {

            $this->success('登录成功', url("admin/Index/index"));
        }

    }

    /**
     * 退出登陆 并清除session.
     *
     * @return void
     */
    public function logout()
    {
        if (isset($_SESSION['Msg'])) {
            $login = $_SESSION['Msg'];
            // 更改状态

            setCookie("cu_com", "", time() - 60);
            $_SESSION['Msg'] = null;
        }


        $this->redirect(url('admin/login/index'));

    }

    /**
     *  socket_auth 验证
     * @return string
     */
    public function auth()
    {

        $sarr = parse_url(ahost);
        if ($sarr['scheme'] == 'https') {
            $state = true;
        } else {
            $state = false;
        }

        $app_key = app_key;
        $app_secret = app_secret;
        $app_id = app_id;
        $options = array(
            'encrypted' => $state
        );
        $host = ahost;
        $port = aport;

        $pusher = new Pusher(
            $app_key,
            $app_secret,
            $app_id,
            $options,
            $host,
            $port
        );


        $data = $pusher->socket_auth($_POST['channel_name'], $_POST['socket_id']);

        return $data;
    }

}
